package com.flute.tools.data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.flute.tools.data.mysql.Constants;

/**
 * 单条数据记录封装类
 * 
 * @author Jazz
 * 
 */
public class DataRecord implements Serializable {

	private static final long serialVersionUID = -1070543050853190777L;

	/**
	 * 数据操作类型枚举
	 * 
	 * 
	 */
	public enum DataAction {
		Add, Update, Delete
	}

	/**
	 * 域名与域值对应关系
	 */
	private Map<String, String> dataMap;
	/**
	 * 数据操作类型
	 */
	private DataAction dataAction;

	public DataRecord() {
		this.dataMap = new HashMap<String, String>();
		this.dataAction = DataAction.Add;
	}

	public String[] getAllFields() {
		return dataMap.keySet().toArray(new String[] {});
	}

	public String getFieldValue(String field) {
		return dataMap.get(field);
	}

	public void deleteField(String field) {
		this.dataMap.remove(field);
	}

	public boolean containsField(String field) {
		return dataMap.containsKey(field);
	}

	public void setAction(DataAction action) {
		this.dataAction = action;
	}

	public void AddField(String field, String value) {
		this.dataMap.put(field, value);
	}

	public DataAction getDataAction() {
		String value = getFieldValue(Constants.ACTION);
		value = StringUtils.capitalize(value);
		setAction(DataAction.valueOf(value));
		return dataAction;
	}

	public String toString() {
		return dataMap.toString();
	}
}
